<?php
/**
 * Created by PhpStorm.
 * User: zhendong
 * Date: 2017/4/18
 * Time: 18:29
 */
namespace Backend\Controller;
use Think\Controller;

class DataReportController extends BaseController
{
    public function profit(){
        $this->display();
    }

    public function time_data(){
        $data =$_GET;
        if(empty($data['begin']) && empty($data['end'])){
            $data['end'] =date("Y-m-d",strtotime("-1 day"));
            $data['begin']=date("Y-m-d",strtotime("-1 week"));
            $yesterday = date("Y-m-d",strtotime("-1 day"));
            $model =D('price');
            $where = array('a.date_time' =>$yesterday,'a.user_id' =>$_SESSION['id']);
            $result = $model->alias('a')
                ->field('a.date_time,a.code_price as sum,a.code_id,c.code_name,c.code_type,c.apply_id,c.status,b.apply_name')
                ->join('dsp_apply as b  on a.apply_id = b.id','LEFT')
                ->join('dsp_adsense as c  on a.code_id=c.id','LEFT')
                ->where($where)
                ->select();
        }else{
            $model =D('price');
            $where['a.date_time'] = array(array('egt',$data['begin']),array('elt',$data['end']));
            $and =array('a.user_id' =>$_SESSION['id']);
            $result = $model->alias('a')
                ->field('a.date_time,a.code_price as sum,a.code_id,c.code_name,c.code_type,c.apply_id,c.status,b.apply_name')
                ->join('dsp_apply as b  on a.apply_id = b.id','LEFT')
                ->join('dsp_adsense as c  on a.code_id=c.id','LEFT')
                ->where($where)
                ->where($and)
                ->select();
            //echo  $model->getLastSql();
        }

        $this->ajaxReturn(['status' => '1', 'msg' => 'ok', 'data' => $result]);

    }
    public  function trend_map(){
        $data =$_GET;
        if(empty($data['begin']) && empty($data['end'])){
            $data['end'] =date("Y-m-d",strtotime("-1 day"));
            $data['begin']=date("Y-m-d",strtotime("-1 week"));
            $yesterday = date("Y-m-d",strtotime("-1 day"));
            $model =D('price');
            $where = array('a.date_time' => $yesterday,'a.user_id' =>$_SESSION['id']);
            $result = $model->alias('a')
                ->field('a.date_time,sum(a.code_price) as sum')
                ->where($where)
                ->select();
            $results['sum'][0] =$result[0]['sum'] ;
            $results['date_time'][0] =$result[0]['date_time'];
        }else{
            $model =D('price');
            $where['a.date_time'] = array(array('egt',$data['begin']),array('elt',$data['end']));
            $and =array('a.user_id' =>$_SESSION['id']);
            $result = $model->alias('a')
                ->field('a.date_time,sum(a.code_price) as sum')
                ->where($where)
                ->where($and)
                ->group('a.date_time')
                ->select();
                foreach ($result as $k =>$v) {
                    $results['sum'][] =$v['sum'];
                    $results['date_time'][] =$v['date_time'];
                }
        }
        $this->ajaxReturn(['status' => '1', 'msg' => 'ok', 'data' => $results]);
    }
    public function media_data(){

        $this->display();
    }


    public  function  trend_report(){
        $data =$_GET;
        if(empty($data['begin']) && empty($data['end'])){
            $data['end'] =date("Y-m-d",strtotime("-1 day"));
            $data['begin']=date("Y-m-d",strtotime("-1 week"));
            $yesterday = date("Y-m-d",strtotime("-1 day"));

            $model =D('price');
            $where = array('a.date_time' => $yesterday,'a.user_id'=>$_SESSION['id']);
            $results['trend'] = $model->alias('a')
                ->field('a.date_time,a.union_click,a.union_view,a.union_click_rate,a.ad_returns,a.ad_requests,b.apply_name,c.code_name,c.code_type')
                ->join('dsp_apply as b on a.apply_id = b.id','LEFT')
                ->join('dsp_adsense as c on a.code_id = c.id','LEFT')
                ->where($where)
                ->select();

            //******************************************************
            $res = $model->alias('a')
                ->field('a.date_time,sum(a.union_click) as union_click, sum(a.union_view) as union_view,sum(a.union_click_rate) as union_click_rate')
                ->where($where)
                ->select();
            $results['union_click'][0] =$res[0]['union_click'] ;
            $results['union_view'][0] =$res[0]['union_view'];
            $results['union_click_rate'][0] =$res[0]['union_click_rate'];
            $results['date_time'][0] =$res[0]['date_time'];
        }else{
            $model =D('price');
            $where['a.date_time'] = array(array('egt',$data['begin']),array('elt',$data['end']));
            $and =array('a.user_id' =>$_SESSION['id']);
            $results['trend'] = $model->alias('a')
                ->field('a.date_time,a.union_click,a.union_view,a.union_click_rate,a.ad_returns,a.ad_requests,b.apply_name,c.code_name,c.code_type')
                ->join('dsp_apply as b on a.apply_id = b.id','LEFT')
                ->join('dsp_adsense as c on a.code_id = c.id','LEFT')
                ->where($where)
                ->where($and)
                ->group('a.date_time')
                ->select();



            //******************************************************
            $res = $model->alias('a')
                ->field('a.date_time,sum(a.union_click) as union_click, sum(a.union_view) as union_view,sum(a.union_click_rate) as union_click_rate')
                ->where($where)
                ->where($and)
                ->group('a.date_time')
                ->select();
            //echo $model->getLastSql();
            foreach ($res as $k =>$v) {
                $results['union_view'][] =$v['union_view'];
                $results['union_click_rate'][] =$v['union_click_rate'];
                $results['union_click'][] =$v['union_click'];
                $results['date_time'][] =$v['date_time'];
            }
        }
        $this->ajaxReturn(['status' => '1', 'msg' => 'ok', 'data' => $results]);
    }
}